package com.qk.mangement.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qk.common.PageResult;
import com.qk.common.enums.BizEnum;
import com.qk.common.excption.BizException;
import com.qk.entity.Course;
import com.qk.entity.CourseQueryListByPageDto;
import com.qk.mangement.mapper.CourseMapper;
import com.qk.mangement.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * Date:2025/10/13
 * Author:zxc
 * Description
 */
@Service
public class CourseServiceImpl implements CourseService {
    @Autowired
    private CourseMapper courseMapper;
    @Override
    public PageResult<Course> queryListByPage(CourseQueryListByPageDto courseQueryListByPageDto) {
        PageInfo<Course> pageInfo = PageHelper.startPage(courseQueryListByPageDto.getPage(), courseQueryListByPageDto.getPageSize())
                .doSelectPageInfo(() -> courseMapper.queryPage(courseQueryListByPageDto));
        return PageResult.<Course>builder().total(Long.valueOf(pageInfo.getTotal()).intValue()).rows(pageInfo.getList()).build();
    }

    @Override
    public void deleteById(Integer id) {
        if (Objects.isNull(id)) {
            BizException.throwBizException(BizEnum.PARAM_ERROR);
        }
        //查询该课程下是否还有客户,有客户则不允许删除
        Integer countCustomer = courseMapper.isExistCustomer(id);
        if (countCustomer > 0){
            BizException.throwBizException(BizEnum.CUSTOMER_EXIST);
        }
        courseMapper.deleteById(id);

    }

    @Override
    public void addCourse(Course course) {
        course.setCreateTime(LocalDateTime.now());
        course.setUpdateTime(LocalDateTime.now());
        courseMapper.addCourse(course);
    }

    @Override
    public Course queryById(Integer id) {
        Course course = courseMapper.queryById(id);
        return course;
    }

    @Override
    public void updateById(Course course) {
        course.setUpdateTime(LocalDateTime.now());
        courseMapper.updateById(course);
    }

    @Override
    public List<Course> queryAll() {
        List<Course> coursesList = courseMapper.queryAll();
        return coursesList;
    }

    @Override
    public List<Course> queryBySubject(Integer subject) {
        List<Course> coursesList = courseMapper.queryBySubject(subject);
        return coursesList;
    }
}
